﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using System.IO;

namespace ShowroomMobile
{
    public partial class ManagerBanner : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ((AdminSite)Page.Master).SetTitlePage("Banners");
                LoadBannerLeft();
                LoadBannerRight();
            }
        }

        protected void btnSaveBannerLeft_OnClick(object sender, EventArgs e)
        {
            try
            {
                Banners banner = new Banners();
                banner.LoadByPrimaryKey(1);
                banner.s_URL = txtURLBannerLeft.Text.Trim();
                banner.Active = ckbActiveBannerLeft.Checked;
                if (fulBannerLeft.HasFile)
                {
                    string[] validExt = { "jpg", "png", "bmp", "gif" };
                    string filename = string.Empty;
                    if (!validExt.Contains<string>(Path.GetExtension(fulBannerLeft.FileName).ToLower().Replace(".", "")))
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('Star Case', 'Invalid image!', 8000);", 1), true);
                        return;
                    }

                    string sPathBanner = Path.Combine(Server.MapPath("~"), Utilities.GetKeySetting("PathUploadBanner"));
                    DirectoryInfo dInfo = new DirectoryInfo(sPathBanner);
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }

                    filename = System.Guid.NewGuid().ToString() + Path.GetExtension(fulBannerLeft.FileName);
                    fulBannerLeft.SaveAs(Path.Combine(sPathBanner, filename));
                    banner.s_FileName = filename;
                }

                banner.Save();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('Star Case', 'Saved!', 8000);", 1), true);
                LoadBannerLeft();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagerBanner.aspx.cs", "btnSaveBannerLeft_OnClick", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('Star Case', 'Oops! Can not upload banner. Please try again later.', 8000);", 1), true);
            }
        }

        protected void btnSaveBannerRight_OnClick(object sender, EventArgs e)
        {
            try
            {
                Banners banner = new Banners();
                banner.LoadByPrimaryKey(4);
                banner.s_URL = txtURLBannerRight.Text.Trim();
                banner.Active = ckbActiveBannerRight.Checked;
                if (fulBannerRight.HasFile)
                {
                    string[] validExt = { "jpg", "png", "bmp", "gif" };
                    string filename = string.Empty;
                    if (!validExt.Contains<string>(Path.GetExtension(fulBannerRight.FileName).ToLower().Replace(".", "")))
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('Star Case', 'Invalid image!', 8000);", 1), true);
                        return;
                    }

                    string sPathBanner = Path.Combine(Server.MapPath("~"), Utilities.GetKeySetting("PathUploadBanner"));
                    DirectoryInfo dInfo = new DirectoryInfo(sPathBanner);
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }

                    filename = System.Guid.NewGuid().ToString() + Path.GetExtension(fulBannerRight.FileName);
                    fulBannerRight.SaveAs(Path.Combine(sPathBanner, filename));
                    banner.s_FileName = filename;
                }

                banner.Save();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('Star Case', 'Saved!', 8000);", 1), true);
                LoadBannerRight();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagerBanner.aspx.cs", "btnSaveBannerRight_OnClick", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('Star Case', 'Oops! Can not upload banner. Please try again later.', 8000);", 1), true);
            }
        }

        private void LoadBannerLeft()
        {
            try
            {
                Banners banner = new Banners();
                banner.LoadByPrimaryKey(1);
                txtURLBannerLeft.Text = banner.s_URL;
                ckbActiveBannerLeft.Checked = banner.Active;
                imgBannerLeft.ImageUrl = Utilities.GetKeySetting("PathUploadBanner") + "/" + banner.s_FileName;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagerBanner.aspx.cs", "LoadBannerLeft()", ex.StackTrace);
            }
        }

        private void LoadBannerRight()
        {
            try
            {
                Banners banner = new Banners();
                banner.LoadByPrimaryKey(4);
                txtURLBannerRight.Text = banner.s_URL;
                ckbActiveBannerRight.Checked = banner.Active;
                imgBannerRight.ImageUrl = Utilities.GetKeySetting("PathUploadBanner") + "/" + banner.s_FileName;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagerBanner.aspx.cs", "LoadBannerRight()", ex.StackTrace);
            }
        }
    }
}